using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace BTSProjectWizard.Forms
{
	/// <summary>
	/// Summary description for PageContainer.
	/// </summary>
	public class PageContainer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Panel _containerPanel;
		private System.Windows.Forms.Button _next;
		private System.Windows.Forms.Button _previous;
		private System.Windows.Forms.Button _finished;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public PageContainer()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		int _currentPage = 0;
		public DialogResult Show(BTSWizardPage[] pages,IntPtr hwnd)
		{
			_pages = pages;
			ShowPage();
			Control c = Control.FromHandle(hwnd);
			this.StartPosition = FormStartPosition.CenterParent;
			return ShowDialog(c);
			
		}
		BTSWizardPage[] _pages;
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PageContainer));
            this.panel1 = new System.Windows.Forms.Panel();
            this._finished = new System.Windows.Forms.Button();
            this._previous = new System.Windows.Forms.Button();
            this._next = new System.Windows.Forms.Button();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this._containerPanel = new System.Windows.Forms.Panel();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this._finished);
            this.panel1.Controls.Add(this._previous);
            this.panel1.Controls.Add(this._next);
            resources.ApplyResources(this.panel1, "panel1");
            this.panel1.Name = "panel1";
            // 
            // _finished
            // 
            resources.ApplyResources(this._finished, "_finished");
            this._finished.Name = "_finished";
            this._finished.Click += new System.EventHandler(this._finished_Click);
            // 
            // _previous
            // 
            resources.ApplyResources(this._previous, "_previous");
            this._previous.Name = "_previous";
            this._previous.Click += new System.EventHandler(this._previous_Click);
            // 
            // _next
            // 
            resources.ApplyResources(this._next, "_next");
            this._next.Name = "_next";
            this._next.Click += new System.EventHandler(this._next_Click);
            // 
            // splitter1
            // 
            resources.ApplyResources(this.splitter1, "splitter1");
            this.splitter1.Name = "splitter1";
            this.splitter1.TabStop = false;
            // 
            // _containerPanel
            // 
            resources.ApplyResources(this._containerPanel, "_containerPanel");
            this._containerPanel.Name = "_containerPanel";
            // 
            // PageContainer
            // 
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this._containerPanel);
            this.Controls.Add(this.splitter1);
            this.Controls.Add(this.panel1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "PageContainer";
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion
		void ShowPage()
		{
			_next.Enabled = _finished.Enabled =_previous.Enabled =false;
			
			BTSWizardPage page =_pages[_currentPage];
			this.Text = page.Title;
			page.Dock = DockStyle.Fill;
			_containerPanel.Controls.Clear();
			_containerPanel.Controls.Add(page);
			
			if(_currentPage==_pages.Length-1)
			{
				
				_finished.Enabled = true;
				this.AcceptButton = _finished;
			}
			else
			{
				_next.Enabled=true;
				this.AcceptButton = _next;
			}
			if(_currentPage !=0)
				_previous.Enabled  = true;

			
		}
		void ShowPage(bool back)
		{
			if(back)
				_currentPage--;
			else
				_currentPage++;
			ShowPage();
		}
		private void _previous_Click(object sender, System.EventArgs e)
		{
			ShowPage(true);
		}

		private void _finished_Click(object sender, System.EventArgs e)
		{
			bool valid = ProcessSymbols();
			if(valid)
			{
				this.DialogResult= DialogResult.OK;
				this.Close();
			}
			
		}
		bool ProcessSymbols()
		{
			BTSWizardPage page =null;
			bool valid=true;
			for(int i=0;i<_pages.Length;i++)
			{
				page = _pages[i];
				valid = page.ProcessSymbols();
				if(!valid)
					return valid;
			}
			return valid;
		}
		private void _next_Click(object sender, System.EventArgs e)
		{
			ShowPage(false);
		}
	}
}
